package com.cunzhang.server.controller.admin;

import com.cunzhang.common.result.PageResult;
import com.cunzhang.common.result.Result;
import com.cunzhang.pojo.dto.DishDTO;
import com.cunzhang.pojo.dto.DishPageQueryDTO;
import com.cunzhang.pojo.vo.DishVO;
import com.cunzhang.server.service.DishService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> pageQueryDish(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.pageQueryDish(dishPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result queryDishById(@PathVariable("id") Long id){
        DishVO dishVO = dishService.queryDishById(id);

        return Result.success(dishVO);
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        dishService.addDish(dishDTO);

        return Result.success();
    }

    /**
     * 起售停售菜品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("起售，停售菜品")
    public Result changeStatus(@PathVariable Integer status, Long id){
        dishService.changeStatus(status, id);

        return Result.success();
    }


    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result deleteDish(String ids){
        String[] strings = ids.split(",");
        List<Integer> idList = new ArrayList<>();
        for(String dishId : strings){
            idList.add(Integer.valueOf(dishId));
        }
        if(!idList.isEmpty()){
            dishService.deleteDish(idList);
        }

        return Result.success();
    }


}
